package com.finalist.entity;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * 选号段用的区域
 * 
 * @author rainy
 * 
 */
@Entity
@Table(name = "boss_area")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "boss_area_sequence")
public class BossArea extends OrderEntity
{

    private static final long serialVersionUID = -5144485613173337491L;

    private BossArea parent; // 上级区域
    private Set<BossArea> children; // 下级区域
    private String name; // 名称
    private String code; // 编码
    private String noPrefix; // 号段
    private Type type; // 运营商类型
    /**
     * 0 未启用
     * 1 启用 
     */
    private Boolean canCharge;
    
    /** 订单 */
    private Set<Order> orders = new HashSet<Order>();
    
    /**
     * 获取订单
     * 
     * @return 订单
     */
    @OneToMany(mappedBy = "boseArea", fetch = FetchType.LAZY)
    public Set<Order> getOrders()
    {
        return orders;
    }

    /**
     * 设置订单
     * 
     * @param orders
     *            订单
     */
    public void setOrders(Set<Order> orders)
    {
        this.orders = orders;
    }

    /**
     * 运营商
     */
    public enum Type
    {

        /** 中国移动 */
        CMCC,

        /** 中国联通 */
        CUCC,

        /** 中国电信 */
        CTCC
    }

    @ManyToOne(fetch = FetchType.EAGER)
    public BossArea getParent()
    {
        return parent;
    }

    public void setParent(BossArea parent)
    {
        this.parent = parent;
    }

    @JsonProperty
    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
    @OrderBy("order asc")
    public Set<BossArea> getChildren()
    {
        return children;
    }

    public void setChildren(Set<BossArea> children)
    {
        this.children = children;
    }

    @JsonProperty
    @NotEmpty
    @Length(max = 100)
    @Column(nullable = false, length = 100)
    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    @JsonProperty
    @Length(max = 20)
    @Column(length = 20)
    public String getNoPrefix()
    {
        return noPrefix;
    }

    @JsonProperty
    @NotEmpty
    @Length(max = 100)
    @Column(nullable = true, length = 100)
    public String getCode()
    {
        return code;
    }

    public void setCode(String code)
    {
        this.code = code;
    }

    public void setNoPrefix(String noPrefix)
    {
        this.noPrefix = noPrefix;
    }

    @JsonProperty
    @Column(nullable = true)
    public Type getType()
    {
        return type;
    }

    public void setType(Type type)
    {
        this.type = type;
    }

    @Transient
    public Collection<BossArea> getFilterChildren()
    {
        Map<String, BossArea> bossAreaMap = new HashMap<String, BossArea>();
        for (BossArea ba : children)
        {
            bossAreaMap.put(ba.getName(), ba);
        }
        return bossAreaMap.values();
    }

    
	public Boolean getCanCharge() {
		return canCharge;
	}

	public void setCanCharge(Boolean canCharge) {
		this.canCharge = canCharge;
	}

}
